<?php
/**
 * Author: 命中水、
 * Date: 2020/6/13 19:12
 * Email: in1993summer@163.com
 */

namespace App\Repositories\Order\Integral;

use App\Repositories\Repository;

/**
 * Class CashOutOrderRepository
 * @package App\Repositories\Order\Integral
 */
class CashOutOrderRepository extends Repository
{
    /**
     * @return string
     */
    public function model()
    {
        return \App\Models\Order\Integral\CashOutOrderModel::class;
    }

    /**
     * 状态
     */
    const STATUS_PASS = 1; // 审核通过
    const STATUS_PAID = 2; // 已支付
    const STATUS_FAIL = -2; // 提现失败

    /**
     * 获取审核通过订单
     *
     * @return mixed
     */
    public function getApplyPassOrder()
    {
        return $this->model->where('status', self::STATUS_PASS)->get();
    }

    /**
     * @return string
     */
    public function getUniqueTradeNo()
    {
        $tradeNo = '';

        while (1) {

            $tradeNo = str_random(15);

            if (!$this->model->where('partner_trade_no', $tradeNo)->first()) {
                break;
            }

        }

        return $tradeNo;
    }

    /**
     * 获取超时未支付成功订单
     *
     * @param $datetime
     * @return mixed
     */
    public function getOvertimeUnPayOrder($datetime)
    {
        return $this->model->where('apply_time', '<', $datetime)->where('status', self::STATUS_PASS)->get();
    }

}